package com.da.javatest.util;

import java.util.List;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.collections.CollectionUtils;

public class CriteriaSetter {
    /**
     * return true if String not blank. otherwise false.
     *
     * @param value
     * @param criteriaSetter
     * @return
     */
    public static boolean setEqual(String value, Consumer<String> criteriaSetter) {
        if (StringUtils.isNotBlank(value)) {
            criteriaSetter.accept(value);
            return true;
        }
        return false;
    }

    /**
     * return true if Long not null. otherwise false.
     *
     * @param value
     * @param criteriaSetter
     * @return
     */
    public static boolean setEqual(Long value, Consumer<Long> criteriaSetter) {
        if (value != null) {
            criteriaSetter.accept(value);
            return true;
        }
        return false;
    }

    /**
     * return true if Long not null. otherwise false.
     *
     * @param value
     * @param criteriaSetter
     * @return
     */
    public static boolean setEqual(Integer value, Consumer<Integer> criteriaSetter) {
        if (value != null) {
            criteriaSetter.accept(value);
            return true;
        }
        return false;
    }

    /**
     * return true if Long not null. otherwise false.
     *
     * @param value
     * @param criteriaSetter
     * @return
     */
    public static boolean setEqualByte(Integer value, Consumer<Byte> criteriaSetter) {
        if (value != null) {
            criteriaSetter.accept(value.byteValue());
            return true;
        }
        return false;
    }

    /**
     * return true if List not empty. otherwise false.
     *
     * @param list
     * @param criteriaSetter
     * @param <T>
     * @return
     */
    public static <T> boolean setIn(List<T> list, Consumer<List<T>> criteriaSetter) {
        if (CollectionUtils.isNotEmpty(list)) {
            criteriaSetter.accept(list);
            return true;
        }
        return false;
    }

    /**
     * return true if String not blank. otherwise false.
     *
     * @param value
     * @param criteriaSetter
     * @return
     */
    public static boolean setLike(String value, Consumer<String> criteriaSetter) {
        if (StringUtils.isNotBlank(value)) {
            criteriaSetter.accept("%" + value + "%");
            return true;
        }
        return false;
    }
}
